package com.qf.zmt.controller;


import com.qf.common.WxPay.MyWXConfig;
import com.qf.common.WxPay.WXPay;
import com.qf.common.http.Result;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.Orders;
import com.qf.entity.po.LoginMerchant;
import com.qf.entity.vo.OrderVo;
import com.qf.service.impl.GoodsService;
import com.qf.service.impl.GoodsSkuService;
import com.qf.service.impl.OrderService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("order")
public class OrderController {

    @Resource
    OrderService orderService;
    @Resource
    GoodsService goodsService;
    @Resource
    GoodsSkuService goodsSkuService;


    @RequestMapping("new")
    public String toNewOrder(Model model, Long goodsId){
        model.addAttribute("goods",goodsService.getGoodsById(goodsId));
        model.addAttribute("buyerNum",orderService.getBuyNumByGoodsId(goodsId));
        model.addAttribute("skuList",goodsSkuService.getByGoodsId(goodsId));
        return "order/new";
    }

    @RequestMapping("createOrder")
    @ResponseBody
    public Result<?> createOrder(Orders order,LoginMerchant loginMerchant){
        orderService.saveOrder(order,loginMerchant);
        return Result.success();
    }

    @RequestMapping("list")
    public String toList(Model model,LoginMerchant loginMerchant){
        List<OrderVo> list = orderService.getOrderByMerchantId(loginMerchant.getId());
        model.addAttribute("orderVoList",list);
        return "order/list";
    }

    @RequestMapping("wxpay")
    public String toWxPay(Model model,@RequestParam(value = "id",required = true)Long orderId){
        model.addAttribute("order",orderService.getById(orderId));
        return "order/wxpay";
    }

    @RequestMapping("getQR")
    @ResponseBody
    public Result<?> getWxQrCode(LoginMerchant loginMerchant,@RequestParam(value = "id",required = true)Long orderId){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        try {
            Orders order = orderService.getById(orderId);
            WXPay wxPay = new WXPay(new MyWXConfig());
            Long goodsId = order.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            Map<String, String> data = new HashMap<String, String>();
            data.put("body", goods.getName());  //商品描述
            data.put("out_trade_no", sdf.format(new Date()).concat(loginMerchant.getId().toString()).concat(String.valueOf(RandomUtils.nextInt(10000, 100000)))); //外部订单号
            data.put("fee_type", "CNY"); //币种
            data.put("total_fee", "1"); //金额，单位:分 ! 不要改!!
            data.put("spbill_create_ip", "127.0.0.1"); //终端ip
            data.put("notify_url", "http://zz52uv.natappfree.cc/callback/wxpay/notifyCall"); //回调地址
            data.put("trade_type", "NATIVE");  // 此处指定为扫码支付
            data.put("product_id", goodsId.toString()); //商品id
            data.put("attach", order.getOrderId()); //内部订单号
            try {
                Map<String, String> resp = wxPay.unifiedOrder(data);
                String resultCode = resp.get("result_code");
                if ("FAIL".equals(resultCode)) {
                    return Result.error(resp.get("err_code_des"));
                } else {
                    return Result.success(resp.get("code_url"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("支付失败");
    }
}
